package com.cz.fra.android;


import java.util.ArrayList;
import java.util.List;

import com.cz.fra.android.entity.Comment;
import com.cz.fra.android.entity.Report;
import com.cz.fra.android.logic.UserLogic;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mFactorys;
    List<Comment> mList = new ArrayList<Comment>();
	public CommentListAdapter(Context context, List<Comment> report) {
		mContext = context;
		mFactorys = LayoutInflater.from(context);
		mList = report;
	}
	
	public String getUsernameById(int uid){
		return UserLogic.getInstance(mContext).getUser(uid).getUsername();
	}
	
	public void addItem(final Comment comment){
		mList.add(comment);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		if (arg1 == null) {
			arg1 = mFactorys.inflate(R.layout.comment_item, null);
		} 
		setView(arg1, arg0);
		return arg1;
	}
	
	static class ViewHolder {
		private ImageView profileImage;
		private TextView commentAuthor;
		private TextView commentTime;
		private TextView commentContent;
    }
	
	private final void setView(final View convertView, final int arg0) {
		ViewHolder viewHolder;
		if (null == convertView.getTag()) {
			viewHolder = new ViewHolder();
			viewHolder.profileImage = (ImageView) convertView.findViewById(R.id.profile_image);
			viewHolder.commentAuthor = (TextView) convertView.findViewById(R.id.comment_author);
			viewHolder.commentTime = (TextView) convertView.findViewById(R.id.comment_time);
			viewHolder.commentContent = (TextView) convertView.findViewById(R.id.comment_content);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(!getProfileImageByUserID(mList.get(arg0).getCommentBy()).equals("null")){
			viewHolder.profileImage.setImageURI(Uri.parse(getProfileImageByUserID(mList.get(arg0).getCommentBy())));
		}
		viewHolder.commentAuthor.setText(getUsernameById(mList.get(arg0).getCommentBy()));
		viewHolder.commentTime.setText(mList.get(arg0).getCommentDate());
		viewHolder.commentContent.setText(mList.get(arg0).getContent());
	}
	

	private String getProfileImageByUserID(int uid){
		return UserLogic.getInstance(mContext).getUser(uid).getProfileImage();
	}
	
	private String parseImagePaths(String imagePaths){
		return imagePaths.substring(0, imagePaths.indexOf(";"));
	}
}
